<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
  .box {
        width: 200px;
        height: 60px;
        border: 1px solid red;
    }
    </style>
  
</head>
<body>
    <!--网址：https://www.bootcdn.cn/axios/ -->
    <!-- 引入axios格式 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.js"></script> 
    <div >jq点击发送请求</div>
    <button id="btn1">get请求</button>
    <button id="btn2">post</button>
    <button id="btn3">通用型方法 ajax</button>
    <div class="box"></div>
    <script>
        const btn = document.querySelectorAll('button')
        const box = document.querySelector('.box')
        btn[0].addEventListener('click',function(){
            // get请求
            axios.get('http://localhost:3000/axios', {
                // url参数
                params: {
                    id: 1,
                    name:'zs'
                },
                //请求头信息
                Headers: {
                    name: 'xm',
                    age: 10
                }
            }).then(value => {
                console.log(value); //响应结果
                console.log(value.data);
                box.innerHTML = value.data
            })
        })

        // post请求
        btn[1].addEventListener('click',function(){
            // post请求
            axios.post('http://localhost:3000/axios', {
                // 请求体信息
                    user: 'z',
                    password: 123
                },{
                // url参数
                params: {
                    id: 1,
                    name:'zs'
                },
                //请求头信息
                Headers: {
                    name: 'xm',
                    age: 10
                },
                
            }).then(value => {
                console.log(value); //响应结果
                console.log(value.data);
                box.innerHTML = value.data
            })
        })

        //通用型方法 发送ajax请求
        btn[2].addEventListener('click',function() {
            axios({
                method: 'post', //请求类型
                url: 'http://localhost:3000/axios', //url路径
                params: {  //url参数
                    a: 1,
                    b: 2
                },
                 //请求头信息
                 Headers: {
                    name: 'xm',
                    age: 10
                },
                 //请求体信息
                 data: {
                    user: 'z',
                    password: 123
                },
            }).then(value => {
                console.log(value); //响应结果
                console.log(value.data);
                box.innerHTML = value.data
            })
        })
    </script>
</body>
</html>